using Microsoft.Phone.Info;
using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Media;
namespace MicroMsg.Common.Utils
{
	public static class DebugEx
	{
		public static long timeCur;
		public static int MainThreadID;
		private static Dictionary<string, long> mapStamp = new Dictionary<string, long>();
		private static Dictionary<string, int> mapStampCount = new Dictionary<string, int>();
		public static void DumpVisualTree(DependencyObject parent, int left = 0)
		{
			int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
			for (int i = 0; i < childrenCount; i++)
			{
				DependencyObject child = VisualTreeHelper.GetChild(parent, i);
				DebugEx.DumpVisualTree(child, left + 1);
			}
		}
		public static void dumpMemoryUsed()
		{
			object obj = 0;
			DeviceExtendedProperties.TryGetValue("DeviceTotalMemory", ref obj);
			object obj2 = 0;
			DeviceExtendedProperties.TryGetValue("ApplicationCurrentMemoryUsage", ref obj2);
			object obj3 = 0;
			DeviceExtendedProperties.TryGetValue("ApplicationPeakMemoryUsage", ref obj3);
			if (obj2 != null && obj3 != null)
			{
				Log.smethod_5("DebugExtensions", "Total Device Memory Used: " + ((long)obj / 1048576L).ToString());
				Log.smethod_5("DebugExtensions", "Total App Memory Used: " + ((long)obj2 / 1048576L).ToString());
				Log.smethod_5("DebugExtensions", "App Peak Memory Used: " + ((long)obj3 / 1048576L).ToString());
			}
		}
		public static void dumpClass(Type type_0)
		{
			MemberInfo[] members = type_0.GetMembers();
			for (int i = 0; i < members.Length; i++)
			{
				MemberInfo memberInfo = members[i];
				MemberTypes memberType = memberInfo.get_MemberType();
				if (memberType <= 16)
				{
					switch (memberType)
					{
					case 1:
					case 2:
					case 3:
					case 4:
						break;
					default:
						if (memberType != 8 && memberType != 16)
						{
						}
						break;
					}
				}
				else
				{
					if (memberType <= 64)
					{
						if (memberType != 32 && memberType != 64)
						{
						}
					}
					else
					{
						if (memberType != 128)
						{
						}
					}
				}
			}
		}
		public static void dumpFields(Type type_0)
		{
			FieldInfo[] fields = type_0.GetFields(60);
			for (int i = 0; i < fields.Length; i++)
			{
				FieldInfo fieldInfo = fields[i];
				MemberTypes memberType = fieldInfo.get_MemberType();
				if (memberType <= 16)
				{
					switch (memberType)
					{
					case 1:
					case 2:
					case 3:
					case 4:
						break;
					default:
						if (memberType != 8 && memberType != 16)
						{
						}
						break;
					}
				}
				else
				{
					if (memberType <= 64)
					{
						if (memberType != 32 && memberType != 64)
						{
						}
					}
					else
					{
						if (memberType != 128)
						{
						}
					}
				}
			}
		}
		public static string debugString(this object object_0)
		{
			string text = "";
			FieldInfo[] fields = object_0.GetType().GetFields(20);
			FieldInfo[] array = fields;
			for (int i = 0; i < array.Length; i++)
			{
				FieldInfo fieldInfo = array[i];
				if (fieldInfo.get_MemberType() == 4)
				{
					object obj = text;
					text = string.Concat(new object[]
					{
						obj,
						fieldInfo.get_Name(),
						"={",
						fieldInfo.GetValue(object_0),
						"} "
					});
				}
			}
			PropertyInfo[] properties = object_0.GetType().GetProperties(20);
			PropertyInfo[] array2 = properties;
			for (int j = 0; j < array2.Length; j++)
			{
				PropertyInfo propertyInfo = array2[j];
				object obj2 = text;
				text = string.Concat(new object[]
				{
					obj2,
					propertyInfo.get_Name(),
					"={",
					propertyInfo.GetValue(object_0, null),
					"} "
				});
			}
			return text;
		}
		public static string debugString(this List<string> strList)
		{
			string text = "";
			using (List<string>.Enumerator enumerator = strList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					string current = enumerator.get_Current();
					text = text + current + " ";
				}
			}
			return text;
		}
		public static string debugString(this List<Type> typeList)
		{
			string text = "";
			using (List<Type>.Enumerator enumerator = typeList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					Type current = enumerator.get_Current();
					text = text + current.get_Name() + " ";
				}
			}
			return text;
		}
		public static void dumpTableName(this DataContext dataContext_0)
		{
			int num = 0;
			IEnumerable<MetaTable> enumerable = Enumerable.Select<MetaTable, MetaTable>(dataContext_0.get_Mapping().GetTables(), (MetaTable metaTable_0) => metaTable_0);
			using (IEnumerator<MetaTable> enumerator = enumerable.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					MetaTable current = enumerator.get_Current();
					using (IEnumerator<MetaDataMember> enumerator2 = current.get_RowType().get_PersistentDataMembers().GetEnumerator())
					{
						while (enumerator2.MoveNext())
						{
							MetaDataMember arg_5D_0 = enumerator2.get_Current();
						}
					}
					num++;
				}
			}
		}
		public static void dumpTable<TEntity>(this Table<TEntity> table) where TEntity : class
		{
			IQueryable<TEntity> queryable = Queryable.Select<TEntity, TEntity>(table, (TEntity m) => m);
			if (Queryable.Count<TEntity>(queryable) <= 0)
			{
				return;
			}
			int num = 0;
			using (IEnumerator<TEntity> enumerator = queryable.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					TEntity arg_4B_0 = enumerator.get_Current();
					num++;
				}
			}
		}
		public static void objectCopy(this object object_0, object object_1)
		{
			if (object_1 == null || object_0 == null)
			{
				return;
			}
			if (object_0.GetType() != object_1.GetType())
			{
				return;
			}
			PropertyInfo[] properties = object_1.GetType().GetProperties();
			PropertyInfo[] array = properties;
			for (int i = 0; i < array.Length; i++)
			{
				PropertyInfo propertyInfo = array[i];
				propertyInfo.SetValue(object_0, propertyInfo.GetValue(object_1, null), null);
				Log.smethod_5("objectCopy property ", propertyInfo.get_Name());
			}
			FieldInfo[] fields = object_1.GetType().GetFields(20);
			FieldInfo[] array2 = fields;
			for (int j = 0; j < array2.Length; j++)
			{
				FieldInfo fieldInfo = array2[j];
				if (fieldInfo.get_MemberType() == 4)
				{
					fieldInfo.SetValue(object_0, fieldInfo.GetValue(object_1));
				}
				Log.smethod_5("objectCopy field ", string.Concat(new object[]
				{
					fieldInfo.get_Name(),
					" ",
					fieldInfo.GetValue(object_1),
					" ",
					fieldInfo.GetValue(object_0)
				}));
			}
		}
		public static void debugBreak()
		{
			if (Debugger.get_IsAttached())
			{
				Debugger.Break();
			}
		}
		public static double getTimeSpan()
		{
			long num = DebugEx.timeCur;
			DebugEx.timeCur = DateTime.get_Now().get_Ticks();
			return TimeSpan.FromTicks(DebugEx.timeCur - num).get_TotalMilliseconds();
		}
		public static long getTimeSpan(long tick)
		{
			if (tick == 0L)
			{
				return DateTime.get_Now().get_Ticks();
			}
			return (long)TimeSpan.FromTicks(DateTime.get_Now().get_Ticks() - tick).get_TotalMilliseconds();
		}
		public static bool IsMainThread()
		{
			return DebugEx.MainThreadID == Thread.get_CurrentThread().get_ManagedThreadId();
		}
		public static void checkThread()
		{
			if (DebugEx.MainThreadID != Thread.get_CurrentThread().get_ManagedThreadId() && Debugger.get_IsAttached())
			{
				Debugger.Break();
			}
		}
		public static void DumpAttributes(MemberInfo member)
		{
			object[] customAttributes = member.GetCustomAttributes(true);
			for (int i = 0; i < customAttributes.Length; i++)
			{
			}
		}
		public static string stringFromUTF8(this byte[] byte_0)
		{
			if (byte_0 == null)
			{
				return "";
			}
			return Encoding.get_UTF8().GetString(byte_0, 0, byte_0.Length);
		}
		public static void timeStart(string stampName, int total = 0)
		{
			DebugEx.mapStamp.set_Item(stampName, DateTime.get_Now().get_Ticks());
			if (total > 0)
			{
				DebugEx.mapStampCount.set_Item(stampName, total);
			}
		}
		public static void timeStamp()
		{
			using (Dictionary<string, long>.Enumerator enumerator = DebugEx.mapStamp.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					KeyValuePair<string, long> current = enumerator.get_Current();
					if (DebugEx.mapStampCount.ContainsKey(current.get_Key()))
					{
						Dictionary<string, int> dictionary;
						string key;
						(dictionary = DebugEx.mapStampCount).set_Item(key = current.get_Key(), dictionary.get_Item(key) - 1);
					}
					double totalMilliseconds = TimeSpan.FromTicks(DateTime.get_Now().get_Ticks() - current.get_Value()).get_TotalMilliseconds();
					Log.smethod_4("###", current.get_Key() + " time=" + totalMilliseconds);
				}
			}
			List<string> list = Enumerable.ToList<string>(Enumerable.Select<KeyValuePair<string, int>, string>(Enumerable.Where<KeyValuePair<string, int>>(DebugEx.mapStampCount, (KeyValuePair<string, int> keyValuePair_0) => keyValuePair_0.get_Value() <= 0), (KeyValuePair<string, int> keyValuePair_0) => keyValuePair_0.get_Key()));
			if (list.get_Count() <= 0)
			{
				return;
			}
			using (List<string>.Enumerator enumerator2 = list.GetEnumerator())
			{
				while (enumerator2.MoveNext())
				{
					string current2 = enumerator2.get_Current();
					DebugEx.mapStamp.Remove(current2);
					DebugEx.mapStampCount.Remove(current2);
				}
			}
		}
		public static void timeStamp(string stampName)
		{
			if (!DebugEx.mapStamp.ContainsKey(stampName))
			{
				return;
			}
			double totalMilliseconds = TimeSpan.FromTicks(DateTime.get_Now().get_Ticks() - DebugEx.mapStamp.get_Item(stampName)).get_TotalMilliseconds();
			Log.smethod_4("###", stampName + " time=" + totalMilliseconds);
		}
	}
}
